package com.kt.uokos.yimo.controller;


import com.kt.uokos.yimo.anno.validator.Order;
import com.kt.uokos.yimo.anno.validator.Sort;
import com.kt.uokos.yimo.dao.Brand;
import com.kt.uokos.yimo.service.BrandService;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auth:kanghao
 * @Disbtrit:请求用户反馈接口Controller
 *
 */
@RestController
@RequestMapping("/admin/brand")
public class BrandController {

    @Resource
    private BrandService brandService;


    @GetMapping("/list")
    public Object list(Integer id, String name, @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        //根据前端传的参数进行用户反馈信息的查询  返回List列表
        List<Brand> brands = brandService.querySelective(id, name, page, size, sort, order);
        return ResponseUtil.okList(brands);
    }

    @PostMapping("/create")
    public Object creat(@RequestBody Brand brand) {
        if (StringUtils.isEmpty(brand.getName())) {
            return ResponseUtil.errs();
        }
        if (StringUtils.isEmpty(brand.getDesc())) {
            return ResponseUtil.errs();
        }
        if (StringUtils.isEmpty(brand.getFloorPrice())) {
            return ResponseUtil.errs();
        }
        brandService.create(brand);
        return ResponseUtil.ok(brand);
    }


    @GetMapping("/read")
    public Object read(Integer Id){
        Brand brand = brandService.findById(Id);
        return ResponseUtil.ok(brand);
    }

    @PostMapping("/delete")
    //接收前端传递的品牌商类从而进行逻辑删除
    public Object delete(@RequestBody Brand brand){
        Integer id = brand.getId();
        if (id==null){
            return ResponseUtil.errs();
        }
        brandService.deleteById(id);
        return ResponseUtil.ok();
    }

    //通过比较前端传过来的brand与之前是否一致 如不一致则直接返回
    @PostMapping("/update")
    public Object update(@RequestBody Brand brand){
        if (StringUtils.isEmpty(brand.getName())) {
            return ResponseUtil.errs();
        }
        if (StringUtils.isEmpty(brand.getDesc())) {
            return ResponseUtil.errs();
        }
        if (StringUtils.isEmpty(brand.getFloorPrice())) {
            return ResponseUtil.errs();
        }
        if (brandService.updateById(brand)==0){
            return ResponseUtil.errs();
        }
       return ResponseUtil.ok();
    }

}